package client.voteclient;

import java.awt.*;
import java.awt.event.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import javax.swing.BoxLayout;

public class VotingClient extends Frame {

    private Button btSend = new Button("Send");
    private Button btQuit = new Button("Quit");
    private Label srvAddrLabel = new Label("Server Adress:");
    private TextField srvAddrField = new TextField("localhost", 40);
    private Label srvPortLabel = new Label("Server Port:");
    private TextField srvPortField = new TextField("2000", 40);
    private Label pollIdLabel = new Label("Pool ID:");
    private TextField pollIdField = new TextField("", 40);
    private Label voteOptLabel = new Label("Vote choice:");
    private TextField voteOptField = new TextField("", 40);
    private Panel ccPanel;

    public VotingClient() {
        super("Vote Client");

        Panel pollIdPanel = new Panel(new BorderLayout());
        Panel voteOptPanel = new Panel(new BorderLayout());
        Panel srvAddrPanel = new Panel(new BorderLayout());
        Panel srvPortPanel = new Panel(new BorderLayout());

        pollIdPanel.add(pollIdLabel, BorderLayout.WEST);
        pollIdPanel.add(pollIdField, BorderLayout.CENTER);
        voteOptPanel.add(voteOptLabel, BorderLayout.WEST);
        voteOptPanel.add(voteOptField, BorderLayout.CENTER);
        srvAddrPanel.add(srvAddrLabel, BorderLayout.WEST);
        srvAddrPanel.add(srvAddrField, BorderLayout.CENTER);
        srvPortPanel.add(srvPortLabel, BorderLayout.WEST);
        srvPortPanel.add(srvPortField, BorderLayout.CENTER);


        Panel fieldPanel = new Panel();
        fieldPanel.setLayout(new BoxLayout(fieldPanel, BoxLayout.PAGE_AXIS));
        fieldPanel.add(pollIdPanel);
        fieldPanel.add(voteOptPanel);
        fieldPanel.add(srvAddrPanel);
        fieldPanel.add(srvPortPanel);
        /* Create a panel for the buttons and add listeners to the
         buttons. */
        Panel buttonPanel = new Panel(new GridLayout(1, 0));
        btSend.addActionListener(new SendListener());
        btQuit.addActionListener(new QuitListener());
        buttonPanel.add(btSend);
        buttonPanel.add(btQuit);
        /* Add, pack, and show. */
        add(fieldPanel, BorderLayout.NORTH);
        add(buttonPanel, BorderLayout.SOUTH);
        pack();
        show();
    }

    static public void main(String argv[]) {
        new VotingClient();
    }

    class SendListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent event) {
            String srvAddr = srvAddrField.getText();
            int port = 0;
            Long pollId = null;
            Long voteOption = null;
            try {
                port = Integer.valueOf(srvPortField.getText());
                pollId = Long.valueOf(pollIdField.getText());
                voteOption = Long.valueOf(voteOptField.getText());
            } catch (Exception e) {
                //TODO : error
                e.printStackTrace();
                System.exit(1);
            }

            DatagramSocket socket = null;
            DatagramPacket packet;
            try {
                socket = new DatagramSocket();
            } catch (SocketException e) {
                //TODO : error
                e.printStackTrace();
                System.exit(1);
                
            }

            try {
                byte msg[] = (pollId.toString() + " " + voteOption.toString()).getBytes();
                System.err.println(new String(msg));
                System.err.println(msg.length);
                packet = new DatagramPacket(msg, msg.length, InetAddress.getByName(srvAddr), port);
                socket.send(packet);
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
    }

    class QuitListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }
}